Imports System.Web.UI.WebControls.WebParts
Imports System
Imports System.Web
Imports System.Collections
Imports System.Globalization
Imports System.Web.Util

Class PersonalizationProviderHelper

    Friend Shared Function CheckAndTrimNonEmptyStringEntries(ByVal array() As String, ByVal paramName As String, ByVal throwIfArrayIsNull As Boolean, ByVal checkCommas As Boolean, ByVal lengthToCheck As Integer) As String()
        If (array Is Nothing) Then
            If throwIfArrayIsNull Then
                Throw New ArgumentNullException(paramName)
            Else
                Return Nothing
            End If
        End If
        If (array.Length = 0) Then
            Throw New ArgumentException("PersonalizationProviderHelper Empty Collection, " & paramName)
        End If
        Dim result() As String = Nothing
        Dim i As Integer = 0
        Do While (i < array.Length)
            Dim str As String = array(i)
            Dim trimmedStr As String = (str Is Nothing)
            If String.IsNullOrEmpty(trimmedStr) Then
                Throw New ArgumentException("PersonalizationProviderHelper Null Or Empty String Entries, " & paramName)
            End If
            If (checkCommas _
                        AndAlso (trimmedStr.IndexOf(Microsoft.VisualBasic.ChrW(44)) <> -1)) Then
                Throw New ArgumentException("PersonalizationProviderHelper Cannot Have Comma In String, " & paramName & ", " & str)
            End If
            If ((lengthToCheck > -1) _
                        AndAlso (trimmedStr.Length > lengthToCheck)) Then
                Throw New ArgumentException("PersonalizationProviderHelper Trimmed Entry Value Exceed Maximum Length, " & str & ", " & paramName & ", " & lengthToCheck.ToString(CultureInfo.CurrentCulture))
            End If
            If (str.Length <> trimmedStr.Length) Then
                If (result Is Nothing) Then
                    result = New String((array.Length) - 1) {}
                    array.Copy(array, result, i)
                End If
            End If
            If (Not (result) Is Nothing) Then
                result(i) = trimmedStr
            End If
            i = (i + 1)
        Loop
        Return result
    End Function

    Friend Shared Function CheckAndTrimStringWithoutCommas(ByVal paramValue As String, ByVal paramName As String) As String
        Dim trimmedValue As String = CheckAndTrimString(paramValue, paramName)
        If (trimmedValue.IndexOf(Microsoft.VisualBasic.ChrW(44)) <> -1) Then
            Throw New ArgumentException("PersonalizationProviderHelper Cannot Have Comma In String, " & paramName & ", " & paramValue)
        End If
        Return trimmedValue
    End Function

    Friend Shared Sub CheckOnlyOnePathWithUsers(ByVal paths() As String, ByVal usernames() As String)
        If ((Not (usernames) Is Nothing) _
                    AndAlso ((usernames.Length > 0) _
                    AndAlso ((Not (paths) Is Nothing) _
                    AndAlso (paths.Length > 1)))) Then
            Throw New ArgumentException("PersonalizationProviderHelper More Than One Path, " & paths.ToString & ", " & usernames.ToString)
        End If
    End Sub

    Friend Shared Sub CheckNegativeInteger(ByVal paramValue As Integer, ByVal paramName As String)
        If (paramValue < 0) Then
            Throw New ArgumentException("PersonalizationProviderHelper Negative Integer, " & paramName)
        End If
    End Sub

    Friend Shared Sub CheckNegativeReturnedInteger(ByVal returnedValue As Integer, ByVal methodName As String)
        If (returnedValue < 0) Then
            Throw New HttpException("PersonalizationAdmin Unexpected PersonalizationProvider ReturnValue, " & returnedValue.ToString(CultureInfo.CurrentCulture) & ", " & methodName)
        End If
    End Sub

    Friend Shared Sub CheckNullEntries(ByVal array As ICollection, ByVal paramName As String)
        If (array Is Nothing) Then
            Throw New ArgumentNullException(paramName)
        End If
        If (array.Count = 0) Then
            Throw New ArgumentException("PersonalizationProviderHelper Empty Collection, " & paramName)
        End If
        For Each item As Object In array
            If (item Is Nothing) Then
                Throw New ArgumentException("PersonalizationProviderHelper Null Entries, " & paramName)
            End If
        Next
    End Sub

    Friend Shared Sub CheckPageIndexAndSize(ByVal pageIndex As Integer, ByVal pageSize As Integer)
        If (pageIndex < 0) Then
            Throw New ArgumentException("PersonalizationProviderHelper Invalid Less Than Parameter, " & pageIndex & ", 0")
        End If
        If (pageSize < 1) Then
            Throw New ArgumentException("PersonalizationProviderHelper Invalid Less Than Parameter, " & pageSize & ", 1")
        End If
        Dim upperBound As Long = ((CType(pageIndex, Long) * pageSize) _
                    + (pageSize - 1))
        If (upperBound > Int32.MaxValue) Then
            Throw New ArgumentException("PageIndex PageSize bad")
        End If
    End Sub

    Friend Shared Sub CheckPersonalizationScope(ByVal scope As PersonalizationScope)
        If ((scope < PersonalizationScope.User) _
                    OrElse (scope > PersonalizationScope.Shared)) Then
            Throw New ArgumentOutOfRangeException("scope")
        End If
    End Sub

    Friend Shared Sub CheckUsernamesInSharedScope(ByVal usernames() As String)
        If (Not (usernames) Is Nothing) Then
            Throw New ArgumentException("PersonalizationProviderHelper No Usernames Set In Shared Scope, " & usernames.ToString & ", scope " & PersonalizationScope.Shared.ToString)
        End If
    End Sub

    Friend Overloads Shared Function CheckAndTrimString(ByVal paramValue As String, ByVal paramName As String) As String
        Return CheckAndTrimString(paramValue, paramName, True)
    End Function

    Friend Overloads Shared Function CheckAndTrimString(ByVal paramValue As String, ByVal paramName As String, ByVal throwIfNull As Boolean) As String
        Return CheckAndTrimString(paramValue, paramName, throwIfNull, -1)
    End Function

    Friend Overloads Shared Function CheckAndTrimString(ByVal paramValue As String, ByVal paramName As String, ByVal throwIfNull As Boolean, ByVal lengthToCheck As Integer) As String
        If (paramValue Is Nothing) Then
            If throwIfNull Then
                Throw New ArgumentNullException(paramName)
            End If
            Return Nothing
        End If
        Dim trimmedValue As String = paramValue.Trim
        If (trimmedValue.Length = 0) Then
            Throw New ArgumentException("PersonalizationProviderHelper Trimmed Empty String, " & paramName)
        End If
        If ((lengthToCheck > -1) _
                    AndAlso (trimmedValue.Length > lengthToCheck)) Then
            Throw New ArgumentException("StringUtil Trimmed String Exceed Maximum Length, " & paramValue & ", " & paramName & ", " & lengthToCheck.ToString(CultureInfo.InvariantCulture))
        End If
        Return trimmedValue
    End Function
End Class
